//
//  EmotionGroupDato.swift
//  Tinsecret_TW
//
//  Created by wanghui on 2018/11/27.
//  Copyright © 2018年 tst. All rights reserved.
//

import Foundation
import WCDBSwift

class EmotionGroupDao {
    
    static func getAllEmotionGroups() throws -> [EmotionGroup]{
        var returnData = [EmotionGroup]()
        let db = SQLiteHelper.getDb()
        let emotionGroups:[EmotionGroup] = try db.getObjects(on: EmotionGroup.CodingKeys.all, fromTable: EmotionGroup.table)
        returnData = emotionGroups
        return returnData
    }
    
    static func getEmotionGroup(id:Int64) throws -> EmotionGroup? {
        var returnData:EmotionGroup?
        let db = SQLiteHelper.getDb()
        let emotionGroup:EmotionGroup? = try db.getObject(on: EmotionGroup.CodingKeys.all, fromTable: EmotionGroup.table, where: EmotionGroup.CodingKeys.id == id)
        returnData = emotionGroup
        return returnData
    }
    
    static func save(emotionGroup:EmotionGroup...) throws {
        try save(emotionGroup: emotionGroup)
    }
    
    static func save(emotionGroup:[EmotionGroup]) throws {
        let db = SQLiteHelper.getDb()
        try db.insert(emotionGroup, intoTable: EmotionGroup.table)
    }
    
    static func delete(id:Int64) throws {
        let db = SQLiteHelper.getDb()
        try db.delete(fromTable: EmotionGroup.table, where: EmotionGroup.CodingKeys.id == id)
    }
}
